﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.Metro;

namespace QLHPHP
{
    public partial class DanhSachSVControl : UserControl
    {
        public DanhSachSVControl()
        {
            InitializeComponent();
        }

        private AppCommands _Commands;

        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                buttonDong.Command = newValue.DanhSachSVCommands.Cancel;
            }
            else
            {
                buttonDong.Command = null;
            }
        }

        private void binding()
        {
            txtHoTen.DataBindings.Clear();
            txtHoTen.DataBindings.Add("Text", dgvDSSV.DataSource, "HoTen");

            txtNgaySinh.DataBindings.Clear();
            txtNgaySinh.DataBindings.Add("Text", dgvDSSV.DataSource, "NgaySinh");

            txtGioiTinh.DataBindings.Clear();
            txtGioiTinh.DataBindings.Add("Text", dgvDSSV.DataSource, "GioiTinh");
           
            txtDienThoai.DataBindings.Clear();
            txtDienThoai.DataBindings.Add("Text", dgvDSSV.DataSource, "SoDT");

            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", dgvDSSV.DataSource, "Email");

            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", dgvDSSV.DataSource, "DiaChi");

            txtMaSinhVien.DataBindings.Clear();
            txtMaSinhVien.DataBindings.Add("Text", dgvDSSV.DataSource, "MSSV");

            txtMaHuyen.DataBindings.Clear();
            txtMaHuyen.DataBindings.Add("Text", dgvDSSV.DataSource, "MaHuyen");

            txtMaDoiTuong.DataBindings.Clear();
            txtMaDoiTuong.DataBindings.Add("Text", dgvDSSV.DataSource, "MaDoiTuong");

            txtMaNganh.DataBindings.Clear();
            txtMaNganh.DataBindings.Add("Text", dgvDSSV.DataSource, "MaNganh");

        }

        private void DanhSachSVControl_Load(object sender, EventArgs e)
        {

            LoadDSSinhVien();
            binding();
            lblTongSV.DataBindings.Clear();
            lblTongSV.DataBindings.Add("Text", SinhVien.TongSinhVien(), "TongSV");
        }

        public void LoadDSSinhVien()
        {
            dgvDSSV.DataSource = SinhVien.LayDSSinhVien();
        }
        private void btnThemSinhVien_Click(object sender, EventArgs e)
        {
            btnThemSinhVien.Command = _Commands.NhapSinhVienCommands.New;
        }

        private void btnXoaSinhVien_Click(object sender, EventArgs e)
        {
            string id = dgvDSSV.CurrentRow.Cells[0].Value.ToString();
            string name = dgvDSSV.CurrentRow.Cells[1].Value.ToString();
            if (id != "")
            {
                if ((MessageBoxEx.Show("Bạn có chắc chắn muốn xóa sinh viên này !", "Xác nhận",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK))
                {
                    try
                    {
                        SinhVien.XoaSinhVien(id);
                        dgvDSSV.DataSource = SinhVien.LayDSSinhVien();
                        ToastNotification.Show(this, "Đã xóa sinh viên " + name + " thành công");
                    }
                    catch
                    {
                        ToastNotification.Show(this, "Lỗi!");
    
                    }
                }
                else
                {
                    ToastNotification.Show(this, "Lỗi!");

                }
            }
            else
            {
                ToastNotification.Show(this, "Vui lòng chọn sinh viên để xóa!");
 
            }
        }

        private void btnCapNhatDSSV_Click(object sender, EventArgs e)
        {
            LoadDSSinhVien();
            binding();
            ToastNotification.Show(this,"Cập nhật sinh vien thành công!");
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            txtHoTen.Text = "";
            txtNgaySinh.Text = "";
            txtGioiTinh.Text = "";
            txtMaHuyen.Text = "";
            txtMaDoiTuong.Text = "";
            txtMaNganh.Text = "";
            txtDienThoai.Text = "";
            txtDiaChi.Text = "";
            txtEmail.Text = "";
        }

        private void btnSuaSinhVien_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text!= "" && txtGioiTinh.Text != ""&& txtMaNganh.Text !="" && txtNgaySinh.Text!="")
            {
                int mssv = int.Parse(txtMaSinhVien.Text);
                try
                {
                    DateTime ngaysinh = DateTime.Parse(txtNgaySinh.Text);
                    SinhVien.CapNhatSinhVien(mssv,txtHoTen.Text, ngaysinh, txtGioiTinh.Text, txtMaHuyen.Text, txtMaDoiTuong.Text,
                                                txtMaNganh.Text, txtDienThoai.Text, txtDiaChi.Text, txtEmail.Text);
                    //dgvDSSV.DataSource = SinhVien.LayDSSinhVien();
                    ToastNotification.Show(this, "Sửa đối tượng thành công!");
                }
                catch
                {
                    ToastNotification.Show(this, "Vui lòng kiểm tra các giá trị nhập liệu!");
                }
            }
            else
            {
                ToastNotification.Show(this, "Không bỏ trống các ô giá trị!");
            }
        }

        private void dgvDSSV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDSSV.Rows[0].Cells[0].Value != null)
            {
                
                try
                {
                    int id = int.Parse(dgvDSSV.CurrentRow.Cells[0].Value.ToString());
                    txtSPDK.DataBindings.Clear();
                    txtSPDK.DataBindings.Add("Text", HocPhi.LaySoPhieuDKMoiNhatTuMSSV(id), "SPMN");
                    
                    txtSoTCTL.DataBindings.Clear();
                    txtSoTCTL.DataBindings.Add("Text", MonHoc.TongTCTuMonHocTichLuy(id), "TongTCTL");
            
                }
                catch
                {
                    binding();
                }
               
            }
        }

        
        
        private void btnXemCT_Click(object sender, EventArgs e)
        {
            btnXemCT.Command = _Commands.MonHocTichLuyCommands.New;
            BienToanCuc.bientoancuc = (this.txtMaSinhVien.Text);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if(txtNoiDung.Text=="")
            {
                ToastNotification.Show(this, "Chưa nhập nội dung tìm kiếm!");
            }
            else
            {
                try
                {
                    dgvDSSV.DataSource = TimKiem.TimKiemHoTenTrongDS(txtNoiDung.Text);
                    binding();
                }
                catch
                {
                }
            }
        }

        private void buttonDong_Click(object sender, EventArgs e)
        {

        }

        
    }
}
